package com.zxbit.flash.presenter;

import android.location.Location;

import com.zxbit.flash.BuildConfig;
import com.zxbit.flash.base.BasePresenter;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.OrderMvpView;
import com.zxbit.flash.network.CustomConsumer;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.response.BlankResponse;
import com.zxbit.flash.response.CheckOrderResponse;
import com.zxbit.flash.response.CreateOrderResponse;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class OrderPresenter extends BasePresenter {
    private OrderMvpView mvpView;

    public OrderPresenter(OrderMvpView mvpView) {
        this.mvpView = mvpView;
    }

    public void checkOrderQualifications() {
        toSubScribe(req.checkOrderQualifications(BuildConfig.channel, UserInfo.getToken(), ParamsFactory.getCommonParams(null)), new CustomConsumer<CheckOrderResponse>() {
            @Override
            public void call(CheckOrderResponse checkOrderResponse) {
                mvpView.onGetDataNext(checkOrderResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    /**
     * 提单接口
     * @param mBorrowMoney
     * @param mPid
     */
    public void createOrder(String mBorrowMoney, String mPid) {
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("expiryReturned", mBorrowMoney.trim());
        formMap.put("pId", mPid.trim());
//        formMap.put("uuid", UserInfo.getSaveUuid());
        formMap.put("userUuid", UserInfo.getSaveUuid());
        toSubScribe(req.createOrder(BuildConfig.channel, UserInfo.getToken(), ParamsFactory.getCommonParams(formMap)), new CustomConsumer<CreateOrderResponse>() {
            @Override
            public void call(CreateOrderResponse response) {
                mvpView.onGetDataNext(response);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    /**
     * 上传位置信息
     * @param location
     */
    public void uploadLocation(Location  location) {
        Map<String, Object> map = new HashMap<>();
        if (location == null) return;
        map.put("longitude", String.valueOf(location.getLongitude()));
        map.put("latitude", String.valueOf(location.getLatitude()));
        map.put("saveType", "2");
        map.put("userPhone", "");
        map.put("userUuid", UserInfo.getSaveUuid());
        toSubScribe(req.uploadLocation(UserInfo.getToken(), ParamsFactory.getCommonParams(map)), new CustomConsumer<BlankResponse>() {
            @Override
            public void call(BlankResponse response) {
                mvpView.onGetDataNext();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });

    }


}
